<!--
  @name: 文章管理
  @author: lizibin
 -->
<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam" :data-callback="dataCallback">
      <!-- 表格 header 按钮 -->
      <!-- <template #tableHeader="scope">
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增</el-button>
      </template> -->
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="success" link @click="preview(scope.row)">预览</el-button>
        <el-button type="success" link @click="openDrawer('审核文章', scope.row)">审核</el-button>
        <el-button type="success" link @click="editDrawer(scope.row)">编辑</el-button>
        <el-button type="primary" link @click="deleteAccount(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <ArticleDrawer ref="drawerRef" />
    <el-dialog v-model="dialogVisible" title="预览" width="1300px" top="50px">
      <div v-highlight class="view" v-html="content"></div>
    </el-dialog>
    <el-dialog v-model="editDialogVisible" title="编辑文章" width="1300px" top="50px">
      <Edit v-if="editDialogVisible" v-on:closeDialog="closeDialog" :valueHtml="editData"></Edit>
    </el-dialog>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { getArticle, deleteArticle, examineLink, updateArticlePass, updateArticleInfo } from "@/api/modules/allApi";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import { ElMessage, ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import ArticleDrawer from "@/views/proTable/components/ArticleDrawer.vue";
import Edit from "./edit.vue";
import { ProTableInstance, ColumnProps, HeaderRenderScope } from "@/components/ProTable/interface";

const router = useRouter();

// ProTable 实例
const proTable = ref<ProTableInstance>();
const dialogVisible = ref(false);
const editDialogVisible = ref(false);
const content = ref("");
const editData = ref(null);
const editDrawer = row => {
  editData.value = row;
  editDialogVisible.value = true;
};
const preview = row => {
  dialogVisible.value = true;
  content.value = row.articleContent;
};

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({});

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    list: data.data,
    total: data.total,
    pageNum: data.currentPage,
    pageSize: data.pageSize
  };
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  let newParams = JSON.parse(JSON.stringify(params));
  newParams.userType = 0;
  newParams.currentPage = newParams.pageNum;
  newParams.userId = 1;
  delete newParams.endTime;
  delete newParams.startTime;
  delete newParams.pageNum;
  return getArticle(newParams);
};

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { prop: "userName", label: "作者" },
  {
    prop: "articleTitle",
    label: "标题"
  },
  {
    prop: "articleImgLitimg",
    label: "缩略图",
    render: scope => {
      return (
        <div>
          <el-image style="width: 100px; height: 100px" src={scope.row.articleImgLitimg} />
        </div>
      );
    }
  },
  {
    prop: "articleClassifyName",
    label: "分类"
  },
  {
    prop: "articleState",
    label: "文章状态",
    render: scope => {
      return <div>{scope.row.articleState == 1 ? "所有人可见" : "仅自己可见"}</div>;
    }
  },
  {
    prop: "review",
    label: "评论数量"
  },
  {
    prop: "commentState",
    label: "评论状态",
    render: scope => {
      return <div>{scope.row.commentState == 1 ? "开启" : "关闭"}</div>;
    }
  },
  {
    prop: "click",
    label: "浏览量"
  },
  {
    prop: "articlePass",
    label: "审核状态",
    render: scope => {
      return (
        <div>
          {scope.row.articlePass == 1 ? (
            <span style="color:#E6A23C">待审核</span>
          ) : scope.row.articlePass == 2 ? (
            <span style="color:#67C23A">已通过</span>
          ) : (
            <span style="color:#F56C6C">已拒绝</span>
          )}
        </div>
      );
    }
  },
  { prop: "operation", label: "操作", fixed: "right", width: 200 }
]);

// 删除信息
const deleteAccount = async (params: any) => {
  await useHandleData(deleteArticle, { articleId: params.articleId }, `删除【${params.articleTitle}】`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<any> | null>(null);
const openDrawer = (title: string, row: Partial<any> = {}) => {
  const params = {
    title,
    isView: false,
    row: {
      articleId: row.articleId,
      articlePass: row.articlePass + ""
    },
    api: updateArticlePass,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};

const closeDialog = () => {
  editDialogVisible.value = false;
};
</script>
<style lang="scss">
.sty_link {
  color: rgb(47 167 185);
}
</style>
